package org.lgiavedoni.android.franq.util.network;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import lombok.Cleanup;

import org.lgiavedoni.android.franq.service.log.Logger;

public class NetworkUtil {

	public static String getMyIp() {
		try {
			InetAddress ownIP = InetAddress.getLocalHost();
			return ownIP.getHostAddress();
		} catch (UnknownHostException e) {
			return "127.0.0.1";
		}
	}

	public static String getResponse(URL url) {
		try {
			InputStream is = (InputStream) url.getContent();
			if (is != null) {
				StringBuilder sb = new StringBuilder();
				String line;

				try {
					@Cleanup
					BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
					while ((line = reader.readLine()) != null) {
						sb.append(line).append("\n");
					}
				} finally {
					is.close();
				}
				return sb.toString();
			} else {
				return null;
			}

		} catch (Exception ex) {
			Logger.inst.error(ex);
		}
		return null;
	}

}
